package com.veo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.veo.entity.Book;
import com.veo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/adminBook")
public class AdminBookController {

    @Autowired
    private BookService bookService;


    /**
     * 后台登录
     */
    @RequestMapping("/admin")
    public String admin(){
        return "/admin/bookManage";
    }

    /**
     * 返货后台管理的所有图书
     */
    @ResponseBody
    @GetMapping("/getAllBook")
    public List<Book> getAllBook(){
        List<Book> list = bookService.list();
        return list;
    }

    /**
     * 检查isbn是否重复
     * @param isbn
     * @return
     */
    @RequestMapping("/searchIsbn")
    @ResponseBody
    public Map<String,Object> searchIsbn(String isbn){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("isbn",isbn);
        Book book = bookService.getOne(wrapper);
        if (book != null){
            map.put("msg","error");
            return map;
        }
        map.put("msg","ok");
        return map;
    }


    /**
     * 添加图书
     */
    @ResponseBody
    @RequestMapping("/addBook")
    public Map<String, Object>  addBook(Book book){
        HashMap<String, Object> map = new HashMap();
        boolean flag = bookService.save(book);
        return returnMsg(flag);
    }

    /**
     * 文件上传
     * @param uploadFile
     * @return
     */
    @PostMapping("/fileUpload")
    @ResponseBody
    @Async
    public Map<String, Object> uploadFile(@RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        HashMap<String, Object> map = new HashMap();
        if (uploadFile.isEmpty()){
            map.put("error","no");
            return map;
        }
        File directory = new File("src/main/resources/static/images");
        String paths = directory.getCanonicalPath();

        String fileName = uploadFile.getOriginalFilename();
        File dest = new File(paths + "/" + fileName);
        if (dest.exists()){
            String uuid = UUID.randomUUID().toString().toLowerCase(Locale.ROOT).replace("-","");
            File file = new File(paths + "/" + uuid + "-" + fileName);
            System.out.println("文件重名");
            uploadFile.transferTo(file);
            map.put("success","oko");
            map.put("fileName",file.getName());
            return map;
        }
        try {
            uploadFile.transferTo(dest);
            map.put("success","ok");
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            return map;
        }
    }

    /**
     * 修改图书
     */
    @ResponseBody
    @RequestMapping("/updateBook")
    public Map<String, Object>  updateBook(Book book){
        HashMap<String, Object> map = new HashMap();
        boolean flag = bookService.updateById(book);
        return returnMsg(flag);
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @RequestMapping("/deleteBookById")
    @ResponseBody
    public Map<String, Object>  deleteBookById(Integer id){
        HashMap<String, Object> map = new HashMap();
        boolean flag = bookService.removeById(id);
        return returnMsg(flag);
    }

    /**
     * 多条记录删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBookByIds")
    @ResponseBody
    public Map<String, Object>  deleteBookById(String ids){
        HashMap<String, Object> map = new HashMap();
        String[] isids = ids.split(",");
        boolean flag = bookService.removeByIds(Arrays.asList(isids));
        return returnMsg(flag);
    }

    private Map<String, Object> returnMsg(boolean flag){
        Map<String,Object> map = new HashMap<>();
        if (flag){
            map.put("msg","ok");
            return map;
        }
        map.put("error","no");
        return map;
    }
}
